package com.xiaoqi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xiaoqi.constant.MessageConstant;

import com.xiaoqi.pojo.Menu;
import com.xiaoqi.pojo.Result;
import com.xiaoqi.pojo.Role;
import com.xiaoqi.service.MenuService;
import com.xiaoqi.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @author PC
 * creationTime: 2022/12/1 13:57
 */
@RequestMapping("/menu")
@RestController
public class MenuController {

    @Reference
    private UserService userService;
    @Reference
    private MenuService menuService;

    /**
     * 更具角色id查询对应的菜单
     * @param
     * @return
     */
    @GetMapping("/findmenu")
    public Result findmenu(){
        //通过SecurityContextHolder获取当前登入用户的信息
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //通过用户名查询对应的角色
        com.xiaoqi.pojo.User user1 = userService.findByName(user.getUsername());
        //获取该用户所有的角色
        Set<Role> roles = user1.getRoles();
        List<Menu> menuList=null;
        if (null!=roles){
            for (Role role : roles) {
                menuList = menuService.getMenu(role.getId());
            }
        }
        return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuList);
    }
}
